using System;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Serialization;

namespace SolarWinds.Logic
{
    [XmlInclude(typeof(Ship))]
    [XmlInclude(typeof(Weapon))]
    [XmlInclude(typeof(Cargo))]
    [XmlInclude(typeof(ProjectileData))]
    public class GameData
    {
        public List<Ship> Ships = new List<Ship>();
        public List<Weapon> Weapon = new List<Weapon>();
        public List<Cargo> Cargo = new List<Cargo>();
        public List<ProjectileData> Projectiles = new List<ProjectileData>();

        public void Save(string filename)
        {
            var serializer = new XmlSerializer(typeof(GameData));
            var stream = new XmlTextWriter(filename, null);

            try
            {
                serializer.Serialize(stream, this);
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            stream.Close();
        }

        public static GameData Load(string filename)
        {
            var serializer = new XmlSerializer(typeof(GameData));
            var stream = new XmlTextReader(filename);
            var result = (GameData)serializer.Deserialize(stream);
            
            stream.Close();

            return result;
        }
    }
}